package com.example.cs_jump.Util;

import android.util.Log;
import android.util.Patterns;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UrlFilter {
    private Document document;
    private Elements elements;
    private Element element;
    private String[] imageur;


    String url = "https://www.csgowiki.top/utility/";

    public UrlFilter(String id) throws Exception {
        this.document = Jsoup.connect(url + id).get();
        element = document.getElementById("markdown");

    }




    public String getChinese() {
        String str1;
        String str = element.html();
        //Log.d("msg", str + "");
       // Log.d("msg", "------------------------------------------------------------------------------------------------------");
        str1 = str.replaceAll("[\\[\\]\\!\\(\\)\\。\\，\\*\\-]+", "");
        //"[^(\\u4e00-\\u9fa5)\\(\\)\\()]"
        String regEx = "[^(\\u4e00-\\u9fa5)]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str1);
//        return str1.replaceAll("[a-zA-Z0-9\\:\\/\\.\\(\\)]+","");
        return m.replaceAll("").trim();
    }

    public String getUrl() {
        String str = element.html();
        String regEx = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        while (m.find()) {
            sb.append(m.group()+"\r\n");
        }
        // Log.d("msg",matcher.group());
        return sb.toString();
    }

    public String throwMethod() {
        StringBuffer sb = new StringBuffer();
        Elements elements = document.getElementsByClass("badge badge-primary");
        sb.append(elements.html() + "\r\n");
        Elements elements1 = document.getElementsByClass("badge badge-success");
        sb.append(elements1.html() + "\r\n");
        Elements elements2 = document.getElementsByClass("badge badge-info");
        sb.append(elements2.html() + "\r\n");
        return sb.toString();
    }

    public String getAuthor() {
        Elements links = document.select("span>a");
        //System.out.println(links.toString());
        String s = links.html().trim();
        System.out.print(s.replaceAll("[$Lv0-9\\n]", "") + "-------------作者");
        // String[] result =s.split("[\\n]");
        //  System.out.println(result[1]);
        return s.replaceAll("[$Lv0-9\\n]", "");
    }
}
